package cn.imokay.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

//循环栅栏，集齐7颗龙珠召唤神龙
public class CyclicBarrierDemo {
    private static final int NUMBER = 7;

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(NUMBER, () -> {
            System.out.println("集齐7颗龙珠,开始召唤神龙");
        });
        for (int i = 0; i < NUMBER; i++) {
            int finalI = i+1;
            new Thread(() -> {
                System.out.println("收到第"+finalI+"颗");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            },"线程"+finalI).start();
        }
    }
}
